package com.example.tongue.ui.Online.Body;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tongue.ui.Online.Body.utils.ScoreCalculationEight;
import com.example.tongue.R;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Peacebody extends Activity implements View.OnClickListener {
    private Button next1,whetherPeace;
    private RadioGroup energy,tired,weak,unhappy,cold,adapt,sleep,forgetful;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.peacebody);
        //控件绑定资源

        next1=findViewById(R.id.Next1);
            energy=findViewById(R.id.Energy);
            tired=findViewById(R.id.Tired);
            weak=findViewById(R.id.Weak);
            unhappy=findViewById(R.id.Unhappy);
            cold=findViewById(R.id.Cold);
            adapt=findViewById(R.id.Adapt);
            sleep=findViewById(R.id.Sleep);
            forgetful=findViewById(R.id.Forgetful);


        //设置监听器

        next1.setOnClickListener(this);

                energy.setOnClickListener(this);
                tired.setOnClickListener(this);
                weak.setOnClickListener(this);
                unhappy.setOnClickListener(this);
                cold.setOnClickListener(this);
                adapt.setOnClickListener(this);
                sleep.setOnClickListener(this);
                forgetful.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.Next1:{
                //计算结果,把结果写入缓存
                PeaceResult();
//                Toast.makeText(Peacebody.this, "进入气虚质判断", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Peacebody.this, QiDeficiency.class));
            }
                
            
        }
    }
    public void PeaceResult(){
         int energyid = energy.getCheckedRadioButtonId();
            RadioButton energybtn = findViewById(energyid);
            String energyfin = energybtn.getText().toString();

            int tiredid = tired.getCheckedRadioButtonId();
            RadioButton tiredbtn = findViewById(tiredid);
            String tiredfin = tiredbtn.getText().toString();

            int weakid = weak.getCheckedRadioButtonId();
            RadioButton weakbtn = findViewById(weakid);
            String weakfin = weakbtn.getText().toString();

            int unhappyid = unhappy.getCheckedRadioButtonId();
            RadioButton unhappybtn = findViewById(unhappyid);
            String unhappyfin = unhappybtn.getText().toString();

            int coldid = cold.getCheckedRadioButtonId();
            RadioButton coldbtn = findViewById(coldid);
            String coldfin = coldbtn.getText().toString();

            int adaptid = adapt.getCheckedRadioButtonId();
            RadioButton adaptbtn = findViewById(adaptid);
            String adaptfin = adaptbtn.getText().toString();

            int sleepid = sleep.getCheckedRadioButtonId();
            RadioButton sleepbtn = findViewById(sleepid);
            String sleepfin = sleepbtn.getText().toString();

            int forgetfulid = forgetful.getCheckedRadioButtonId();
            RadioButton forgetfulbtn = findViewById(forgetfulid);
            String forgetfulfin = forgetfulbtn.getText().toString();



            Map<String, Integer> energyMap = new HashMap<String, Integer>();
            energyMap.put("没有", 1);
            energyMap.put("很少", 2);
            energyMap.put("有时", 3);
            energyMap.put("常常", 4);
            energyMap.put("经常", 5);

            Map<String, Integer> tiredMap = new HashMap<String, Integer>();
            tiredMap.put("没有", 1);
            tiredMap.put("很少", 2);
            tiredMap.put("有时", 3);
            tiredMap.put("常常", 4);
            tiredMap.put("经常", 5);

            Map<String, Integer> weakMap = new HashMap<String, Integer>();
            weakMap.put("没有", 1);
            weakMap.put("很少", 2);
            weakMap.put("有时", 3);
            weakMap.put("常常", 4);
            weakMap.put("经常", 5);

            Map<String, Integer> unhappyMap = new HashMap<String, Integer>();
            unhappyMap.put("没有", 1);
            unhappyMap.put("很少", 2);
            unhappyMap.put("有时", 3);
            unhappyMap.put("常常", 4);
            unhappyMap.put("经常", 5);

            Map<String, Integer> coldMap = new HashMap<String, Integer>();
            coldMap.put("没有", 1);
            coldMap.put("很少", 2);
            coldMap.put("有时", 3);
            coldMap.put("常常", 4);
            coldMap.put("经常", 5);

            Map<String, Integer> sleepMap = new HashMap<String, Integer>();
            sleepMap.put("没有", 1);
            sleepMap.put("很少", 2);
            sleepMap.put("有时", 3);
            sleepMap.put("常常", 4);
            sleepMap.put("经常", 5);

            Map<String, Integer> adaptMap = new HashMap<String, Integer>();
            adaptMap.put("没有", 1);
            adaptMap.put("很少", 2);
            adaptMap.put("有时", 3);
            adaptMap.put("常常", 4);
            adaptMap.put("经常", 5);


            Map<String, Integer> forgetfulMap = new HashMap<String, Integer>();
            forgetfulMap.put("没有", 1);
            forgetfulMap.put("很少", 2);
            forgetfulMap.put("有时", 3);
            forgetfulMap.put("常常", 4);
            forgetfulMap.put("经常", 5);

            Integer RawScore = energyMap.get(energyfin) +
                    tiredMap.get(tiredfin) +
                    weakMap.get(weakfin) +
                    unhappyMap.get(unhappyfin) +
                    coldMap.get(coldfin) +
                    sleepMap.get(sleepfin) +
                    adaptMap.get(adaptfin) +
                    forgetfulMap.get(forgetfulfin);
        ScoreCalculationEight scoreCalculationEight=new ScoreCalculationEight(RawScore);
        String ConversionScore=scoreCalculationEight.getResult();
        TextView presult = (TextView) findViewById(R.id.PeaceResult);
        presult.setText("平和质得分:" + ConversionScore);


        File dataFileDir=new File(getExternalCacheDir(),"PhysiqueDataFile");

        if(dataFileDir.exists()){
            dataFileDir.delete();
            dataFileDir.mkdir();
        }else {
            dataFileDir.mkdir();
        }
        FileWriter writer=null;
        try {
            //开启写入流
             writer=new FileWriter(dataFileDir+"/PhysiqueData.txt");
             writer.write(ConversionScore+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println("数据文件已生成");
            }
        }



    }
}
